"use strict";
// pages/product/info.js
var common = require('../../../utils/api.js')
var api = common.api
var util = require('../../../utils/util.js')
var toast = require('../../../utils/toast.js')
var app = getApp()
var postdata=[]
function calculate(scroe){
  var result=[0,0,0,0,0]
  if(scroe){
    for(var i=0;i<scroe;i++){
      result[i]=1
    }
  }
  return result
}
Page({
  data:{
    product:{},
    headTitle:'项目打分',
    productstatus:util.productstatus,
    statusselect:'',
    pid:'',
    scroe:[]
  },
  onShow:function(options){

  },
  onReady:function(){
    // 页面渲染完成
  },
  onLoad:function(options){
    // 页面显示
        util.showLoading({title: '加载中'})
    this.toast = toast
    var that = this
    this.data.pid = options.pid
    console.log(app.globalData.userInfo)
    // 页面初始化 options为页面跳转所带来的参数
    api.getscoreat(app.globalData.userInfo.id,options.pid,function(result){
      console.log('getProductInfo',result,that.productstatus,that.headTitle)
      
      that.setScore(result.data.list,result.data.score)
      that.setData({product:result.data})
      util.hideLoading()
    })
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  setScore:function(list,scores){
    var scroe = []
    list.forEach(function(item){
      var userscore = scores.find(function(itemchild){
        return itemchild.userid == item.id
      })
      scroe.push(calculate(userscore && !util.isEmpty(userscore)?userscore.score:null))
    })
    console.log(52,scroe)
    this.setData({scroe:scroe})
  },
  startap:function(event){
    var index =  event.target.dataset.index+1
    var findex =  event.target.dataset.findex
    var uid =  event.target.dataset.uid
    console.log(index,this.data.scroe)
    this.data.scroe[findex] = calculate(index)
    
    this.setData({
      scroe:this.data.scroe
    })
    var score = this.data.scroe[findex].reduce(function(acc, val) {
      return acc + val;
    }, 0);
    var data={
      pid:this.data.pid,
      uid:uid,
      score:score
    }
    var flag = postdata.find(function(item){return item.uid==uid}
    )
    if(flag){
      flag.score = score
    }else{
      postdata.push(data)
    }
    
    // api.score(data,function(res){
    //   if(res.errno==0){
    //     // wx.showToast({
    //     //     title: '打分成功',
    //     //     icon: 'success',
    //     //     duration: 2000,
    //     // })
    //   }
    // })
    
  },
  check:function(score){
    var flag =  true
    for(var i=0;i<score.length;i++){
      var scoretemp = score[i].reduce(function(acc, val) {
      return acc + val;
    }, 0);
      if(scoretemp==0){
        flag = false
      }
    }
    return flag
  },
  submit:function(){
    console.log(this.data.scroe)
    if(this.check(this.data.scroe)){
      console.log('提交')
        api.score({"data":postdata},function(res){
        if(res.errno==0){
          wx.showToast({
              title: '打分成功',
              icon: 'success',
              duration: 2000,
          })
        }
      })
    }
  }
})